{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test the intent classifier and entity extractor\n",
    "This notebook uses the pretrained BiDAF model and BERT model tuned in [bert_train.ipynb notebook](bert_train.ipynb). See the [README.md](../README.md) for instructions on how to use that notebook.\n",
    "\n",
    "## See also:\n",
    "- [bert_train.ipynb](bert_train.ipynb) to train the intent classifier model.\n",
    "- [bert_model_runtime.ipynb](bert_model_runtime.ipynb) to test the BERT intent classifier model.\n",
    "- [bidaf_model_runtime.ipynb](bidaf_model_runtime.ipynb) to test the associated BiDAF model to test the entity extractor model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from model_corebot101.bert.model_runtime.bert_model_runtime import BertModelRuntime\n",
    "from model_corebot101.bidaf.model_runtime.bidaf_model_runtime import BidafModelRuntime\n",
    "import os\n",
    "from pathlib import Path\n",
    "bidaf_model_dir = os.path.abspath(os.path.join(Path.home(), \"models/bidaf\"))\n",
    "bert_model_dir = os.path.abspath(os.path.join(Path.home(), \"models/bert\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "bidaf model directory already present..\n"
     ]
    }
   ],
   "source": [
    "BidafModelRuntime.init_bidaf(bidaf_model_dir, True)\n",
    "bidaf = BidafModelRuntime(\n",
    "    targets=[\"from\", \"to\", \"date\"],\n",
    "    queries={\n",
    "        \"from\": \"which city will you travel from?\",\n",
    "        \"to\": \"which city will you travel to?\",\n",
    "        \"date\": \"which date will you travel?\",\n",
    "    },\n",
    "    model_dir = bidaf_model_dir\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bert = BertModelRuntime(model_dir=bert_model_dir, label_list=[\"Book flight\", \"Cancel\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def serve(utterance):\n",
    "    intent = bert.serve(utterance)\n",
    "    entities = bidaf.serve(utterance)\n",
    "    return intent, entities"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `BertModelRuntime.serve` method\n",
    "The `BertModelRuntime.serve` method is used to perform the classification against the bot utterances."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "serve(\"flight to paris from london on feb 14th\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "serve(\"from seattle to san\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "serve(\"random random random 42\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "serve(\"any\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "serve(\"take me to New York\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "serve(\"we'd like to go to seattle\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "serve(\"not this one\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "serve(\"I don't care about this one\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "serve(\"I don't want to see that\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "serve(\"boring\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "serve(\"you have no clue how to book a flight\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
